//package site.mmda.controller;
//
//import com.tpehr.common.core.web.domain.ResponseResult;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.MediaType;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
//
//import java.io.IOException;
//import java.util.Objects;
//import java.util.concurrent.ConcurrentHashMap;
//
///**
// * @author 廉壮
// * @version 1.0
// * @date 2024-05-28 14:41
// */
//@CrossOrigin
//@Slf4j
//@RestController
//@RequestMapping("/sse")
//public class Asse {
//
//    private final ConcurrentHashMap<String, SseEmitter> pool=new ConcurrentHashMap<>();
//    @GetMapping(path="/stream-sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
////    @GetMapping("/stream-sse")
//    public SseEmitter streamSse() {
//        SseEmitter emitter = new SseEmitter(Long.MAX_VALUE); // 长时间保持连接
//
//        // 在一个单独的线程中发送事件
//        new Thread(() -> {
//            try {
//                int i = 0;
//                while (true) { // 无限循环发送事件，直到连接关闭
//                    String message = "SSE Message " + i++ +"%";
//                    emitter.send(message);
//                    Thread.sleep(10); // 每秒发送一个事件
//                }
//            } catch (InterruptedException | IOException e) {
////                e.printStackTrace();
//            }
//        }).start();
//
//        return emitter;
//    }
//
//    @GetMapping(path = "/subScribe/{id}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public SseEmitter subScribe(@PathVariable("id") String id){
//
//        SseEmitter sseEmitter = pool.get(id);
//        if (Objects.isNull(sseEmitter)){
//            sseEmitter = new SseEmitter(Long.MAX_VALUE);
//            sseEmitter.onCompletion(()->pool.remove(id));
//            sseEmitter.onError(w->pool.remove(id));
//            sseEmitter.onTimeout(()->pool.remove(id));
//            pool.put(id,sseEmitter);
//        }
//        return sseEmitter;
//    }
//    @PostMapping(path = "/publish/{id}/{content}")
//    public ResponseResult publish(@PathVariable("id") String id,
//                                  @PathVariable("content") String content) throws IOException {
//        SseEmitter sseEmitter = pool.get(id);
//        if (Objects.isNull(sseEmitter)){
//            return ResponseResult.success("sse为空");
//        }
//        sseEmitter.send(content);
//        return ResponseResult.success("发送成功"+content);
//    }
//
//
//
//
//}
